package com.sdy.dcsb.biz.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sdy.common.model.Response;
import com.sdy.dcsb.biz.mapper.EsbConsumerTreeMapper;;
import com.sdy.dcsb.biz.model.EsbConsumerSystem;
import com.sdy.dcsb.biz.model.EsbConsumerTree;
import com.sdy.dcsb.biz.service.EsbConsumerSystemService;
import com.sdy.dcsb.biz.service.EsbConsumerTreeService;
import com.sdy.mvc.service.impl.BaseServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 服务消费者树表 服务实现类
 * </p>
 *
 * @author fb
 * @since 2019-09-27
 */
@Slf4j
@Service
public class EsbConsumerTreeServiceImpl extends BaseServiceImpl<EsbConsumerTree> implements EsbConsumerTreeService {
    @Autowired
    private EsbConsumerTreeMapper esbConsumerTreeMapper;
    @Autowired
    private EsbConsumerTreeService esbConsumerTreeService;
    @Autowired
    private EsbConsumerSystemService esbConsumerSystemService;

    @Override
    public Integer createSystemGroup(Integer code, Integer haschildren, Integer lvl, String[] deptName, Integer parId) {
        Integer id = null;
        if(deptName.length == 1) {
            QueryWrapper<EsbConsumerTree> esbConsumerTreeWrapper = new QueryWrapper<>();
            esbConsumerTreeWrapper.ne("code", "0");
            esbConsumerTreeWrapper.eq("haschildren", 0);
            esbConsumerTreeWrapper.eq("lvl", lvl+1);
            esbConsumerTreeWrapper.eq("name", deptName[0]);
            esbConsumerTreeWrapper.eq("par_id", parId);
            List<EsbConsumerTree> esbConsumerTreeResult = esbConsumerTreeService.list(esbConsumerTreeWrapper);
            if(esbConsumerTreeResult.size() != 0){
                // 系统systemid
                return esbConsumerTreeResult.get(0).getCode();
            }
            id = 0;
        }else {
            id = selectContents(code, haschildren, lvl+1, deptName[0], parId);
        }
        if(id == 0) {
            for(int i=0; i<deptName.length; i++){
                lvl = lvl + 1;
                if(i == deptName.length-1) {
                    EsbConsumerSystem esbConsumerSystem = new EsbConsumerSystem();
                    // 随动
                    String systemCode = systemCodeAutoincrement();
                    esbConsumerSystem.setSystemCode(systemCode);
                    esbConsumerSystem.setSystemName(deptName[i]);
                    esbConsumerSystem.setPName("");
                    esbConsumerSystem.setPPhone("");
                    esbConsumerSystem.setManageName(deptName[i]);
                    esbConsumerSystem.setStatus("1");
                    esbConsumerSystemService.save(esbConsumerSystem);
                    code = esbConsumerSystem.getId();
                    insertContents(code, 0, lvl, deptName[i], parId);
                }else {
                    parId = insertContents(code, haschildren, lvl, deptName[i], parId);
                }
            }
            // 系统systemid
            return code;
        }else {
            String[] dept = new String[deptName.length-1];
            for(int i=1; i<deptName.length; i++) {
                dept[i-1] = deptName[i];
            }
            return createSystemGroup(code, haschildren, lvl+1, dept, id);
        }
    }

    /**
     * 查询消费者树目录是否存在
     * @param code
     * @param haschildren
     * @param lvl
     * @param name
     * @param parId
     * @return
     */
    private Integer selectContents(Integer code, Integer haschildren, Integer lvl, String name, Integer parId) {
        QueryWrapper<EsbConsumerTree> esbConsumerTreeWrapper = new QueryWrapper<>();
        if(code == 0) {
            esbConsumerTreeWrapper.eq("code", code);
        }else {
            esbConsumerTreeWrapper.ne("code", "0");
        }
        esbConsumerTreeWrapper.eq("haschildren", haschildren);
        esbConsumerTreeWrapper.eq("lvl", lvl);
        esbConsumerTreeWrapper.eq("name", name);
        esbConsumerTreeWrapper.eq("par_id", parId);
        List<EsbConsumerTree> esbConsumerTreeResult = esbConsumerTreeService.list(esbConsumerTreeWrapper);
        if(esbConsumerTreeResult.size() == 0){
            return 0;
        }else {
            return esbConsumerTreeResult.get(0).getId();
        }
    }

    /**
     * 创建消费者树目录
     * @param code
     * @param haschildren
     * @param lvl
     * @param name
     * @param parId
     * @return
     */
    private Integer insertContents(Integer code, Integer haschildren, Integer lvl, String name, Integer parId) {
        EsbConsumerTree esbConsumerTree = new EsbConsumerTree();
        esbConsumerTree.setCode(code);
        esbConsumerTree.setHaschildren(haschildren);
        esbConsumerTree.setLvl(lvl);
        esbConsumerTree.setName(name);
        esbConsumerTree.setStatus("1");
        esbConsumerTree.setParId(parId);
        esbConsumerTreeService.save(esbConsumerTree);
/*        QueryWrapper<EsbConsumerTree> esbConsumerTreeWrapper = new QueryWrapper<>();
        esbConsumerTreeWrapper.eq("code", code);
        esbConsumerTreeWrapper.eq("haschildren", haschildren);
        esbConsumerTreeWrapper.eq("lvl", lvl);
        esbConsumerTreeWrapper.eq("name", name);
        esbConsumerTreeWrapper.eq("par_id", parId);
        List<EsbConsumerTree> esbConsumerTreeResult = esbConsumerTreeService.list(esbConsumerTreeWrapper);
        Integer id = esbConsumerTreeResult.get(0).getId();*/
        Integer id = esbConsumerTree.getId();
        return id;
    }

    /**
     * 自动生成消费者系统编码,共享申请自动配置权限自动生产消费者系统使用
     * @return
     */
    private String systemCodeAutoincrement() {
        QueryWrapper<EsbConsumerSystem> esbConsumerSystemQueryWrapper = new QueryWrapper<>();
        esbConsumerSystemQueryWrapper.likeRight("system_code", "dir");
        esbConsumerSystemQueryWrapper.orderByDesc("system_code");
        List<EsbConsumerSystem> esbConsumerSystemResult = esbConsumerSystemService.list(esbConsumerSystemQueryWrapper);
        if(esbConsumerSystemResult.size() == 0 ) {
            return "dir.1000";
        }else {
            String systemCode = esbConsumerSystemResult.get(0).getSystemCode();
            Integer systemCodeInteger = Integer.valueOf(systemCode.substring(4));
            systemCodeInteger++;
            systemCode = "dir." + systemCodeInteger.toString();
            return systemCode;
        }
    }

}
